﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Design_Pattners.Composite;


namespace Module_Basic_Entities
{
    /// <summary>
    /// Leaf
    /// </summary>
    public class Layer : MapComponent, ILeaf
    {
        public Layer(List<List<Tile>> list, int mapWidth,
            int mapHeight, List<BLKSTR> blocks)
        {
            this.Tiles = new List<List<Tile>>();
            
            this.helperPosition = new Vector2();
            
            this.tiles = list;
            
            this.MapWidth = mapWidth;
            
            this.MapHeight = mapHeight;

            this.blocks = blocks;
            
            this.NumChildLayers = 1;
        }       

        public override void Init()
        {
            
        }

        public override void Add<T>(T c)
        {
            // do nothing
            //base.Add<T>(c);
        }

        public override void Remove<T>(T c)
        {
            // do nothing
            //base.Remove<T>(c);
        }

        protected List<Rectangle> tileSourceRectangles;

        public List<List<Tile>> tiles;

        /// <summary>
        /// Helper Position for the Map Construction.
        /// </summary>
        public Vector2 helperPosition;

        /// <summary>
        /// Informations of the blocks; User 1, User 2, User 3, ... User7
        /// </summary>
        protected List<BLKSTR> blocks;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">id of the field: Tile.Id </param>
        /// <returns></returns>
        public BLKSTR? GetBlockInformation(int id)
        {
            if (id >= 0 && id < blocks.Count)
            {
                return blocks[id];
            }

            return null;
        }

        #region Properties

        public int MapWidth
        {
            get;
            set;
        }

        public int MapHeight
        {
            get;
            set;
        }

        public List<List<Tile>> Tiles
        {
            get
            {
                return this.tiles;
            }

            set
            {
                this.tiles = value;
            }
        }

        public Texture2D TileStripTexture
        {
            get;
            set;
        }

        #endregion

        /// <summary>
        /// Constroi os "Rectangles" que define os tiles na textura de tiles
        /// </summary>
        /// <param name="linhas">Número de Linhas da malha de Tiles</param>
        /// <param name="colunas">Número de colunas da malha de Tiles</param>
        public void BuildSourceRectanglesTileStrip(int linhas, int colunas)
        {
            this.tileSourceRectangles = new List<Rectangle>();

            int tileWidth = this.TileStripTexture.Width / colunas;
            int tileHeight = this.TileStripTexture.Height / linhas;

            for (int j = 0; j < this.TileStripTexture.Height; j += tileHeight)
            {
                for (int i = 0; i < this.TileStripTexture.Width; i += tileWidth)
                {
                    this.tileSourceRectangles.Add(new Rectangle(i, j, tileWidth, tileHeight));
                }
            }
        }

        public Rectangle GetTileSourceRectangle(int id)
        {
            if (id == 0)
            {
                return Rectangle.Empty;
            }

            if (!(id - 1 < 0 || id - 1 > this.tileSourceRectangles.Count))
            {
                id--;
            }

            return this.tileSourceRectangles[id];
        }

        public override void Draw(float gameTime)
        {
            //throw new NotImplementedException();
        }


    }
}
